package com.wwwc.index.ejb;

import java.io.*;
import java.util.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.index.ejb.language.*;
import com.wwwc.index.ejb.category.*;
import com.wwwc.index.ejb.directory.*;
import com.wwwc.index.ejb.forum.*;
import com.wwwc.index.ejb.admin.*;
import com.wwwc.index.ejb.user.*;

public class EJBManager {
    
    public static Object getEJB (String jndi_name, String ejb_name) {
	Object ejbob = null;
	try {
	    InitialContext initial = new InitialContext();
	    Object objref = initial.lookup(jndi_name);
	    if(ejb_name != null && ejb_name.equals("Language")) {
		LanguageHome lhome = (LanguageHome)PortableRemoteObject.narrow(objref, LanguageHome.class);
		ejbob = (Language)lhome.create();
	    }
	    if(ejb_name != null && ejb_name.equals("Category")) {
		CategoryHome chome = (CategoryHome)PortableRemoteObject.narrow(objref, CategoryHome.class);
		ejbob = (Category)chome.create();
	    }
	    if(ejb_name != null && ejb_name.equals("Directory")) {
		DirectoryHome dhome = (DirectoryHome)PortableRemoteObject.narrow(objref, DirectoryHome.class);
		ejbob = (Directory)dhome.create();
	    }
	    if(ejb_name != null && ejb_name.equals("Forum")) {
		ForumHome fhome = (ForumHome)PortableRemoteObject.narrow(objref, ForumHome.class);
		ejbob = (Forum)fhome.create();
	    }
	    if(ejb_name != null && ejb_name.equals("Admin")) {
		AdminHome ahome = (AdminHome)PortableRemoteObject.narrow(objref, AdminHome.class);
		ejbob = (Admin)ahome.create();
	    }
	    if(ejb_name != null && ejb_name.equals("User")) {
		UserHome uhome = (UserHome)PortableRemoteObject.narrow(objref, UserHome.class);
		ejbob = (User)uhome.create();
	    }
	} 
	catch (RemoteException e) {
	    System.out.println("ManagerEJB:RemoteException:"+e);
	}
	catch (CreateException e) {
	    System.out.println("ManagerEJB:CreateException:"+e);
	} 
	catch (NamingException e) {
	    System.out.println("ManagerEJB:NamingException:"+e);
	}
	return ejbob;
    }
}
